#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/12/30 0030 15:13
# @Author : Ares_Wang
# @Site :
# @File : other_model_train.py
# @Software: PyCharm
from src.utils import *
from keras.optimizers import SGD
from src.model.copy.GoogleNet import GoogleNet
from src.model.copy.AlexNet import AlexNet
import numpy as np

# 测试网络
if __name__ == '__main__':
    test_filename = r'H:\wangjianlian\data\formal_data\HER2\thrid_generation\val\20X\g1'
    batch_size = 100
    if_save_model = True

    # googleNet = GoogleNet()
    # model = googleNet.build()

    alexNet = AlexNet()
    model = alexNet.build()

    model.compile(optimizer=SGD(lr=0.0005, momentum=0.9, nesterov=True),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model_path = r'H:\wangjianlian\project\Python\networkTest\resources\weight\temp\AlexNet\5model_1.h5'
    model.load_weights(model_path)

    data = Data()
    max_metrics = 0
    all_loss = 0
    all_metrics = 0
    for index, trained_imgs, trained_label in data.read_image(test_filename, batch_size, img_width = 227, img_height = 227, shuffle = False):
        x_test = trained_imgs
        y_test = trained_label[0]

        # y_test = np.expand_dims(y_test, axis=1)
        # y_test = np.expand_dims(y_test, axis=1)

        loss_and_metrics = model.evaluate(x_test, y_test, batch_size=batch_size)  # 应该是返回损失值和metrics
        print("测试第%d批，loss值:%f" % (index, loss_and_metrics[0]))
        print("测试第%d批，metrics值:%f" % (index, loss_and_metrics[1]))
        all_loss += loss_and_metrics[0]
        all_metrics += loss_and_metrics[1]

    print("************ 测试：平均值 *************")
    print("测试平均loss值:%f" % (all_loss / index))
    print("测试平均metrics值:%f" % (all_metrics / index))